package com.epoint.mobileoa.personalized.epointcompany.ui;

import java.util.HashMap;

import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.ResKey;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.personalized.epointcompany.bizlogic.AddTripPlanTask;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.ControlsHelp;
import com.epoint.mobileoa.tools.DateHelp;
import com.epoint.mobileoa.tools.LogHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.tools.controls.tree.codeitemtree.CodeItemTree;
import com.epoint.mobileoa.tools.controls.tree.codeitemtree.NewCodeItemTree;
import com.epoint.mobileoa.tools.controls.tree.codeitemtree.TreeDateDealHelp;
import com.epoint.mobileoa.ui.personchoose.PersonChooseViewNew;

/**
 * @author liyc
 * @time 2012-4-25 上午11:20:10
 * @annotation 新增出差计划
 */
public class AddNewTripPlanView extends SuperView {

	private EditText etCcsjfrom,etCcsjto,etTxry,etTxrq,etCcry,etCcdq,etGzlr;
	private Spinner spCcsjfrom,spCcsjto;
	private CheckBox cbBrsfcc,cbSfyc;
	private Button btCcry,btTjbc,btCcdq;
	public static final String[] DAY_TIME = new String[]{
		"00:00","01:00","02:00","03:00","04:00","05:00",
		"06:00","07:00","08:00","09:00","10:00","11:00",
		"12:00","13:00","14:00","15:00","16:00","17:00",
		"18:00","19:00","20:00","21:00","22:00","23:00",
	};

	private String ccrynames,ccryguids;
	private int resumeType;//1出差人员恢复,2出差地区
	private int taskid;//1初始化页面数据 2保存数据
	@Override
	public void initControls() {
		etGzlr = (EditText) findViewById(R.id.etGzlr);
		etCcdq = (EditText) findViewById(R.id.etCcdq);
		btCcdq = (Button) findViewById(R.id.btCcdq);
		btCcdq.setOnClickListener(this);
		btTjbc = (Button) findViewById(R.id.btTjbc);
		btTjbc.setOnClickListener(this);
		btCcry = (Button) findViewById(R.id.btCcry);
		btCcry.setOnClickListener(this);
		etCcry = (EditText) findViewById(R.id.etCcry);
		cbBrsfcc = (CheckBox) findViewById(R.id.cbBrsfcc);
		setTextViewFontColor(cbBrsfcc);
		cbSfyc = (CheckBox) findViewById(R.id.cbSfyc);
		setTextViewFontColor(cbSfyc);
		etTxry = (EditText) findViewById(R.id.etTxry);
		etTxrq = (EditText) findViewById(R.id.etTxrq);
		etCcsjfrom = (EditText) findViewById(R.id.etCcsjfrom);
		etCcsjto = (EditText) findViewById(R.id.etCcsjto);
		etCcsjfrom.setOnClickListener(this);
		etCcsjto.setOnClickListener(this);
		spCcsjfrom = (Spinner) findViewById(R.id.spCcsjfrom);
		spCcsjto = (Spinner) findViewById(R.id.spCcsjto);
		cbBrsfcc.setChecked(true);
		cbBrsfcc.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					if(!ccrynames.contains(fp.getDisplayname())){
						if(ccrynames.length()==0){
							ccrynames += fp.getDisplayname();
							ccryguids += fp.getUserguid();
						}
						else{
							ccrynames += ";"+fp.getDisplayname();
							ccryguids += ";"+fp.getUserguid();
						}
					}
				}
				else{
					if(ccrynames.contains(fp.getDisplayname())){
						if(ccrynames.indexOf(fp.getDisplayname())==0){
							ccrynames = ccrynames.replaceAll(fp.getDisplayname(), "");
							ccryguids = ccryguids.replaceAll(fp.getUserguid(), "");
						}
						else{
							ccrynames = ccrynames.replaceAll(";"+fp.getDisplayname(), "");
							ccryguids = ccryguids.replaceAll(";"+fp.getUserguid(), "");
						}
					}
				}
				if(ccrynames.indexOf(";")==0){
					ccrynames = ccrynames.substring(1, ccrynames.length());
				}
				if(ccryguids.indexOf(";")==0){
					ccryguids = ccryguids.substring(1, ccryguids.length());
				}
				etCcry.setText(ccrynames);
			}
		});
		ccrynames = fp.getDisplayname();
		ccryguids = fp.getUserguid();
		etCcry.setText(ccrynames);
		etTxry.setText(fp.getDisplayname());
		String currentDate = getIntent().getStringExtra("writeDate");
		etCcsjfrom.setText(currentDate);
		etCcsjto.setText(currentDate);
		etTxrq.setText(currentDate);
		ArrayAdapter<String> aa = ControlsHelp.getSPAdapter(this);
		for(String s:DAY_TIME){
			aa.add(s);
		}
		spCcsjfrom.setAdapter(aa);
		spCcsjfrom.setSelection(8);
		spCcsjto.setAdapter(aa);
		spCcsjto.setSelection(17);

		/*if(InitAddWorkContentPageInfoTask.getWorkTypeCodeItemsCount(this,"地区代码")==0){
			showDialog(null);
			taskid = 1;
			Task task = new InitChuchaijihuaTask(this, null);
			MainService.addTask(task);
		}*/
	}

	@Override
	public void onClick(View v) {
		if(v==etCcsjfrom){
			AlertHelp.showDatePickDialog(this, new OnDateSetListener() {

				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					etCcsjfrom.setText(year + "-" + DateHelp.switchDay((monthOfYear+1)) + "-" + DateHelp.switchDay(dayOfMonth));
				}
			},null);
		}
		else if(v==etCcsjto){
			AlertHelp.showDatePickDialog(this, new OnDateSetListener() {

				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					etCcsjto.setText(year + "-" + DateHelp.switchDay((monthOfYear+1)) + "-" + DateHelp.switchDay(dayOfMonth));
				}
			},null);
		}
		else if(v==btCcry){
			resumeType = 1;
			PersonChooseViewNew.clist = StringHelp.String2List(ccrynames, ccryguids);
			Intent it = new Intent(this,PersonChooseViewNew.class);
			startActivity(it);
		}
		else if(v==btTjbc){
			String ccsjfrom = etCcsjfrom.getText().toString()+spCcsjfrom.getSelectedItem().toString();
			String ccsjto = etCcsjto.getText().toString()+spCcsjto.getSelectedItem().toString();
			Long ccsjfrom_int = Long.parseLong(ccsjfrom.replaceAll("-", "").replaceAll(":", ""));
			Long ccsjto_int = Long.parseLong(ccsjto.replaceAll("-", "").replaceAll(":", ""));

			if(ccsjto_int<ccsjfrom_int){
				ToastHelp.showToast(this, "出差截止时间不能在起始时间之前，请重新选择");
				return;
			}

			if(ControlsHelp.Check_EditText_Blank_Warning(etCcdq, "请选择出差地区")){
				return;
			}

			showDialog_wait("正在保存数据...");
			taskid = 2;
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("StartTime", etCcsjfrom.getText().toString());
			param.put("EndTimte", etCcsjto.getText().toString());
			param.put("WorkContent", etGzlr.getText().toString());
			param.put("AddPlanTime", etTxrq.getText().toString());
			param.put("dispName",fp.getDisplayname());
			param.put("UserGuid",fp.getUserguid());
			param.put("Address","");
			param.put("StartTimeHour",spCcsjfrom.getSelectedItem().toString());
			param.put("EndTimeHour",spCcsjto.getSelectedItem().toString());
			param.put("IsLate","");

			String sfyc = "0";
			if(cbSfyc.isChecked()){
				sfyc = "1";
			}
			param.put("IsUseCar",sfyc);

			String IsSelfBTraveling = "0";
			if(cbBrsfcc.isChecked()){
				IsSelfBTraveling = "1";
			}

			param.put("IsSelfBTraveling",IsSelfBTraveling);
			param.put("OuGuid",DBHelp.getUserOuguid(this, fp.getUserguid()));

			String area = etCcdq.getTag().toString();
			param.put("Area",etCcdq.getTag().toString());
			param.put("TripPartners",ccrynames+";");
			param.put("TripPartnersGuid",ccryguids+";");
			param.put("RowGuid","");

			Task task = new AddTripPlanTask(this, param);
			MainService.addTask(task);
		}
		else if(v==btCcdq){
			resumeType = 2;
			//			new NewTree(this, "地区选择", "地区代码", 2, "",null,false,3);
			new NewCodeItemTree(this, ResKey.dqdmtreenode, "地区选择", "地区代码", 2, "", null, false, 3,"GetCode_ItemTreeData");
		}
	}

	@Override
	public void refresh(Object... param) {
		if(param[0].toString().equals(MainService.NORMAL)){
			if(taskid==2){
				String bs = param[1].toString();
				if(bs.contains("<result>true</result>")){
					ToastHelp.showToast(this, "添加保存成功");
					finish();
				}
				else{


					String alert = StringHelp.getXMLAtt(bs, "alert");

					if(!alert.equals("")){
						ToastHelp.showToast(this, alert);
					}
					else{
						ToastHelp.showToast(this, "保存失败");
					}
				}
			}
		}
		m_Dialog.dismiss();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(resumeType==1){
			String[] info = StringHelp.dealChooseListToString(PersonChooseViewNew.clist);
			ccrynames = info[0];
			ccryguids = info[1];
			etCcry.setText(ccrynames);
		}
		else if(resumeType==2){
			TreeDateDealHelp.DealChooseTreeData(etCcdq);
		}

	}


	@Override
	public int getLayoutInt() {
		return R.layout.epoint_addnewtripplan;
	}

	@Override
	public String setTitle() {
		return "新增出差计划";
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		CodeItemTree.chooseNodeData.clear();
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}

}
